$(function ()
{
    findType();
    findAll();
})

function findType()
{
    let data = publivAjax('/back/newTypeList', {}, 'get');

    let list = data.list;
    let html = '<option value="-1" selected>全部</option>\n' +
        '                <option value="0">无</option>';

    if (list != null)
    {
        for (let i = 0; i < list.length; i++)
        {
            html += '<option value="' + list[i].id + '">' + list[i].typeName + '</option>';
        }
    }

    $('#newType').html(html);
}

function findAll()
{
    let msg = {};
    msg.title = $.trim($("#title").val());
    msg.from = $.trim($("#from").val());
    msg.author = $.trim($("#author").val());
    msg.type = $("#type").val();
    msg.content = $("#content").val();
    msg.introduction = $("#introduction").val();
    msg.startBirthday = $("#startBirthday").val();
    msg.endBirthday = $("#endBirthday").val();
    msg.order = $.trim($("#order").val());
    msg.lift = $.trim($("#lift").val());
    msg.pageSize = $.trim($("#pageSize").val());
    msg.pageNum = $.trim($("#pageNum").val());
    msg.nowNum = $.trim($("#nowNum").val());
    msg.isShow = $.trim($("#isShow").val());
    msg.recommend = $.trim($("#recommend").val());
    msg.newType = $("#newType").val();

    let data = publivAjax('/back/new/findAll', msg, 'get');

    let html = '';
    let list = data.list;
    $("#count").html(data.count);
    if (list != null)
    {
        console.log(list);
        for (let i = 0; i < list.length; i++)
        {
            html += '<tr>\n' +
                '                <td>' + list[i].id + '</td>\n' +
                '                <td>' + list[i].title + '</td>\n' +
                '                <td>' + list[i].isFrom + '</td>\n' +
                '                <td>' + list[i].author + '</td>\n' +
                '                <td>' + list[i].type + '</td>\n' +
                '                <td>' + list[i].content + '</td>\n' +
                '                <td>' + list[i].introduction + '</td>\n' +
                '                <td>' + (list[i].isShow == 1 ? '是' : '否') + '</td>\n' +
                '                <td>' + (list[i].recommend == 1 ? '是' : '否') + '</td>\n' +
                '                <td>' + (list[i].newTypeId == 0 ? '无' : list[i].newTypeModel.typeName) + '</td>\n' +
                '                <td>' + list[i].createTime + '</td>\n' +
                '                <td>' + list[i].updateTime + '</td>\n' +
                '                <td>' + list[i].viewNum + '</td>\n' +
                '                <td>\n' +
                '                    <div><a href="javascript:edit(' + list[i].id + ')">编辑</a></div>\n' +
                '                    <div><a href="javascript:del(' + list[i].id + ')">删除</a></div>\n' +
                '                </td>\n' +
                '            </tr>';
        }
    }

    $("#findAll").html(html);
}

function edit(id)
{
    sessionStorage.setItem("newid", id);
    $('#b-right').load('/back/new/newEdit.html')
}


function del(id)
{
    let data = publivAjax('/back/new/del', {id: id}, 'post');

    if (data.result == 1)
    {
        findAll();
    }
}